<!--
 * @FilePath     : /study_code/vue2.x/api/options-provide-inject.html
 * @Description  : vue2.x options provide inject
 * @Date         : 2025-05-06 09:56:17
 * @Author       : Fang YingGui
 * @LastEditors  : fangyinggui 18715384437@163.com
 * @LastEditTime : 2025-05-06 09:56:17
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>documentName</title>
    <link rel="stylesheet" href="" />
  </head>
  <body>
    <div id="app">
      <my-component>
        <child-component></child-component>
      </my-component>
    </div>

    <script src="/vue/dist/vue.min.js"></script>
    <script src="./common.js"></script>
    <script>
      // 自定义组件
      const myComponent = {
        data: function () {
          return {}
        },
        props: [],
        template: `
        <div>
          <p>一级子组件</p>
          <button v-on:click="getMap">inject 使用1</button>
          <slot>插槽默认内容</slot>
        </div>
      `,
        methods: {},
        inject: ['getMap'],
      }

      const childComponent = {
        data: function () {
          return {}
        },
        props: [],
        template: `
        <div>
          <p>二级子组件</p>
          <button v-on:click="getMap">inject 使用1</button>
        </div>
      `,
        methods: {},
        inject: ['getMap'],
      }

      var vm = new Vue({
        el: '#app',
        data: function () {
          return {}
        },
        components: {
          myComponent,
          childComponent,
        },
        methods: {
          getMap() {
            console.log('provide提供的组件共用方法')
          },
        },
        provide() {
          return {
            getMap: this.getMap,
          }
        },
      })
    </script>
  </body>
</html>
